#!/bin/bash
export 'PS4=[`date +"%Y-%m-%d %H:%M:%S"`]`basename $0`.$LINENO+ '
set -ue
source ../lib/repolib.sh
source ../lib/gitlib.sh
source ../lib/lib.sh
source ./config_obs_common.sh
giteeusername=$1
giteepassword=$2

Verify_Prj="home:Admin:verify"
mytab=`echo -e "\t"`
CAPTH=$(cd `dirname $0`;pwd)

MANIFEST="default.xml"
repo_dir="openeuler_next"
obs_meta_path="$repo_root$repo_dir/"

function init_verfiy_prj()
{
cat << EOF > ${Verify_Prj}.meta
<project name="home:Admin:verify">
  <title/>
  <description>openEuler Mainline Extras</description>
  <person userid="Admin" role="maintainer"/>
  <repository name="standard_x86_64">
    <path project="openEuler:Mainline" repository="standard_x86_64"/>
    <arch>x86_64</arch>
  </repository>
</project>
EOF
osc meta prj "$Verify_Prj" --file="${Verify_Prj}.meta"
}

function parse_git_log()
{
    log_message=$1
    temp_log_type=`echo $log_message | awk '{print $1}'`
    file_path=`echo $log_message | awk '{print $2}'`
    file_name=`basename $file_path`

    prj=`echo $file_path | awk -F'/' '{print $2}'`
    if [ "$file_name" == "_meta" ]; then
        pkg=`echo $file_path | awk -F'/' '{print $3}'`
        if [ "$temp_log_type" == "A" -o "$temp_log_type" == "M" ]; then
            log_type="Mod-pkg-meta"
        elif [ "$temp_log_type" == "D" ]; then
            brother_line=`echo $file_path | sed 's#.osc/_meta#_service#g'`
            if ! cat $CAPTH/diff_patch | grep $brother_line; then
                if test -f ${obs_meta_path}/obs_meta/projects/$prj/$pkg/.osc/_meta; then
                    log_error "$log_message :Can not delele _meta only. Please delete the package"
                fi
            fi
        fi
    elif [ "$file_name" == "_service" ]; then
        pkg=`echo $file_path | awk -F'/' '{print $3}'`
        if [ "$temp_log_type" == "D" ]; then
            brother_line=`echo $file_path | sed 's#_service#.osc/_meta#g'`
            if ! cat $CAPTH/diff_patch | grep $brother_line; then
                if test -f ${obs_meta_path}/obs_meta/projects/$prj/$pkg/_service; then
                    log_error "$log_message :Can not delele _service only. Please delete the package"
                fi
            fi
        else 
            log_type='Mod-pkg-service'
        fi
    elif [[ "$file_name" == *.prjconf ]]; then
        if [ "$temp_log_type" == "D" ]; then
            if test -d ${obs_meta_path}/obs_meta/projects/$prj; then
                log_error "$log_message :Can not delete .prjconf only. Please delete the project"
            fi
        else
            log_type='Mod-prj-prjconf'
        fi
    elif [[ "$file_name" == *.meta ]]; then
        if [ "$temp_log_type" == "D" ]; then
            if test -d ${obs_meta_path}/obs_meta/projects/$prj; then
                log_error "$log_message :Can not delete .meta only. Please delete the project"
            fi
        else
            log_type='Mod-prj-meta'
        fi
    else
        log_error "$log_message failed"
    fi
}

function mod_prj_prjconf_verify()
{
    prj=$1
    osc meta prjconf "$Verify_Prj" --file="${obs_meta_path}/obs_meta/projects/${prj}/.osc/${prj}.prjconf"
}

function mod_prj_meta_verify()
{
    prj=$1
    cp "${obs_meta_path}/obs_meta/projects/${prj}/.osc/${prj}.meta" .
    sed -i "/<project/s/${prj}/${Verify_Prj}/g" "${prj}.meta"
    osc meta prj "$Verify_Prj" --file="${prj}.meta"
}

function mod_pkg_service_verify()
{
    prj=$1
    pkg=$2
    if osc ls $Verify_Prj $pkg &>/dev/null; then
        osc rdelete $Verify_Prj $pkg -m "jenkins"
    fi
    rm -rf $Verify_Prj
    pkg_num=`osc ls $Verify_Prj | wc -l`
    if [ $pkg_num -eq 0 ]; then
        osc co $Verify_Prj
    else
        osc co $Verify_Prj `osc ls $Verify_Prj 2>/dev/null | sed -n '1p'`
    fi
    fservice=${obs_meta_path}/obs_meta/projects/$prj/$pkg/_service
    index=`cat $fservice | egrep -n '<service |</service>' | awk -F':' '{print $1}'`
    index_arry=($index)
    index_num=${#index_arry[@]}
    pushd $Verify_Prj
    for((i=0;i<$index_num;i=$i+2))
    do
        scm=`cat $fservice | egrep 'name="scm"' | egrep -v "<\!--.*-->" | awk -F'<|>' '{print $3}'`
        url=`cat $fservice | egrep 'name="url"' | egrep -v "<\!--.*-->" | awk -F'<|>' '{print $3}'`
        if [ "$scm" == "repo" ]; then
            xxrepo_dir=`echo ${url%%/*}`
            xxrepo_path=`echo ${url##*/}`
            if [ ! -d $obs_meta_path$xxrepo_path ]; then
                pushd $repo_root
                git clone https://$giteeusername:$giteepassword@gitee.com/src-openeuler/$xxrepo_path
                popd
            fi
            if [ ! -d $obs_meta_path${xxrepo_path}/.git ]; then
                log_error "$prj $pkg $url 不是一个合法的git库路径"
            fi
        fi
    done
    cp -r ${obs_meta_path}/obs_meta/projects/$prj/$pkg .
    rm -rf $pkg/.osc
    osc add $pkg
    osc ci -m "modify _service by jenkins"
    popd
}

function mod_pkg_meta_verify()
{
    prj=$1
    pkg=$2
    if ! osc ls $Verify_Prj $pkg &>/dev/null; then
        mod_pkg_service_verify $prj $pkg
        sleep 3
        if ! osc ls $Verify_Prj $pkg &>/dev/null; then
            sleep 6
            if ! osc ls $Verify_Prj $pkg &>/dev/null; then
                sleep 9
            fi
        fi
    fi
    cp ${obs_meta_path}/obs_meta/projects/${prj}/${pkg}/.osc/_meta .
    sed -i "s/${prj}/${Verify_Prj}/g" _meta
    if ! osc meta pkg "$Verify_Prj" "$pkg" --file="_meta" | grep ^Done.; then
        log_error "${prj}/${pkg}/.osc/_meta deal error"
    fi
}

function config_obs_by_upload()
{
    set -e
    logtype=$1
    prj=$2
    pkg=$3
    case $logtype in
    Mod-prj-meta)
        mod_prj_meta_verify $prj
    ;;
    Mod-prj-prjconf)
        mod_prj_prjconf_verify $prj
    ;;
    Mod-pkg-service)
        mod_pkg_service_verify $prj $pkg
    ;;
    Mod-pkg-meta)
        mod_pkg_meta_verify $prj $pkg
    ;;
    esac
}

func_main()
{
    check_env
    init_verfiy_prj

    pushd ${obs_meta_path}/obs_meta
    git diff --name-status HEAD~1 HEAD~0 > $CAPTH/diff_patch
    popd

    check_redu_flag=0
    check_prj_flag=0
    rm -f $CAPTH/{check_prj_flag.file,check_redu_flag.file}
    version_prjs=`cat ${obs_meta_path}/obs_meta/obs/version | grep "project name=" | awk -F'"' '{print $2}'`
    version_string=`echo $version_prjs | sed 's# #/|#g'`
    while read line
    do
        if echo "$line" | egrep "projects/[^/ ]*/.osc/.*\.meta|projects/[^/ ]*/.osc/.*\.prjconf" &>/dev/null; then
            echo "工程全局配置文件: $line" >> $CAPTH/check_prj_flag.file
            check_prj_flag=1
        elif ! echo "$line" | egrep "$version_string" &>/dev/null; then
            echo "修改工程不属于该分支，请查看obs/version配置文件"
            exit 1
        elif echo "$line" | egrep "projects/[^/ ]*/[^/ ]*/_service|projects/[^/ ]*/[^/ ]*/\.osc/_meta" &>/dev/null; then
            log_type=''
            prj=''
            pkg=''
            parse_git_log "$line"
            config_obs_by_upload "$log_type" "$prj" "$pkg"
        elif echo "$line" | egrep "projects/[^/ ]*/\.osc/_sources" | grep '^D' &>/dev/null; then
            continue
        else
            echo "冗余的修改文件: $line" >> $CAPTH/check_redu_flag.file
            check_redu_flag=1
        fi
    done < $CAPTH/diff_patch

    if [ $check_prj_flag -ne 0 ]; then
        log_info "修改工程全局配置文件请联系SE/CIE检视 !!!"
        echo "************全局配置文件******************************"
        cat $CAPTH/check_prj_flag.file
        echo "******************************************************"
    fi
    if [ $check_redu_flag -ne 0 ]; then
        log_info "请删除冗余的修改文件 !!!"
        echo "************冗余配置文件******************************"
        cat $CAPTH/check_redu_flag.file
        echo "******************************************************"
    fi

    if [ $check_prj_flag -ne 0 -o $check_redu_flag -ne 0 ]; then
        echo "修改工程配置文件，需要工程进行手动加1 "
        exit 1
    fi
}

func_main
